package com.fr.chicken.manager.playerinfo;

import com.fr.chicken.dao.GoodsLogDao;
import com.fr.chicken.dao.PlayerInfoDao;
import com.fr.chicken.model.PlayerInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class PlayerInfoManager {

    public final static int DEFAULTVIPLV = 1;

    @Autowired
    private PlayerInfoDao playerInfoDao;
    @Autowired
    private GoodsLogDao goodsLogDao;

    /**
     *根据激活码查找玩家信息
     * */
    public PlayerInfo findPlayerInfoByActivationCode(String activationCode){
        return playerInfoDao.selectByActivationCode(activationCode);
    }


    /**
     * 激活码是否存在
     * */
    public boolean  checkActivationExist(String activationCode){
        return goodsLogDao.selectByInfoAndGoodsId(activationCode,1) != null;
    }

    /**
     * 昵称是否存在
     * */
    public boolean checkNickNameExist(String nickName){
        return playerInfoDao.selectByNickName(nickName) !=null;
    }

    /**
     * 根据id 查找用户
     * */
    public PlayerInfo findPlayerInfoById(long playerId){
        return  playerInfoDao.selectByPrimaryKey(playerId);
    }

    /**
     * 创建玩家
     * */
    public PlayerInfo createPlayer(String nickName){
        PlayerInfo playerInfo = new PlayerInfo();
        playerInfo.setNickName(nickName);
        playerInfo.setVip(DEFAULTVIPLV);
        if(!insertPlayerInfo(playerInfo)){
            throw new RuntimeException("插入玩家信息失败");
        }
        return playerInfo;
    }

    /**
     * 新增玩家
     * */
    public boolean insertPlayerInfo(PlayerInfo playerInfo){
        return playerInfoDao.insertSelective(playerInfo) >= 1;
    }

    /**
     * 保存玩家
     * */
    public boolean savePlayerInfo(PlayerInfo player){
        return playerInfoDao.updateByPrimaryKeySelective(player) >= 1;
    }

    /**
     * 给玩家赠送鸡蛋
     * */
    public boolean addEgg(PlayerInfo playerInfo,int egg){
        playerInfo.setEgg(playerInfo.getEgg()+egg);
        return  playerInfoDao.updateByPrimaryKeySelective(playerInfo) >= 1;
    }
}
